package cn.ulyer.common.jpa.entity;

import cn.ulyer.common.json.jsr.CreateGroup;
import cn.ulyer.common.json.jsr.UpdateGroup;
import cn.ulyer.common.model.AbstractBaseModel;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.Date;


@Entity
@Table(name = "t_user",indexes ={@Index(name = "username_idx",columnList = "username",unique = true)} )
@Getter
@Setter
public class User extends AbstractBaseModel {

    @Id
    @Column(columnDefinition = "bigint(20)")
    @GenericGenerator(strategy = "cn.ulyer.common.jpa.IdWorkerGenerator",name="idWorker")
    @GeneratedValue(generator = "idWorker")
    private Long id;

    @Column(columnDefinition = "varchar(128) comment '密码'",nullable = false)
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    @Size(min = 6)
    @NotBlank(groups = {CreateGroup.class})
    private String password;

    @Column(columnDefinition = "varchar(64) comment '帐号'", nullable = false)
    @NotBlank(groups = {CreateGroup.class})
    private String username;

    @Column(columnDefinition = "varchar(64) comment '名称'", nullable = false)
    @NotBlank(groups = {CreateGroup.class})
    @NotBlank(groups = {UpdateGroup.class})
    private String nickname;

    @Column(columnDefinition = "varchar(128) comment '备注'")
    private String remark;

    @Column(columnDefinition = "varchar(256) comment '头像'")
    private String avatar;

    @Column(columnDefinition = "tinyint(1) comment '是否锁定'")
    private Integer accountLocked;

    @Column(columnDefinition = "tinyint(1) comment '是否启用'",nullable = false)
    private Integer enable;

    @Column(columnDefinition = "varchar(11) comment '电话'")
    private String phone;

    @Column(columnDefinition = "varchar(64) comment '邮箱'")
    private String email;

    @Column(columnDefinition = "timestamp comment '帐号到期时间'")
    @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
    private Date expireTime;

    @Column(columnDefinition = "bigint(20) comment '部门ID'", nullable = false)
    @NotNull(groups = {CreateGroup.class})
    @NotNull(groups = {UpdateGroup.class})
    private Long deptId;
}
